<template>
  <div class="pro_con">
    <el-card class="box-card">
      <div class="clearfix">
        <i class="iconfont icon-sousuo-copy"> </i>
        <span> 修改商品类型</span>
      </div>
    </el-card>

    <el-form
      :model="listQuery"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="类型名称:" prop="name">
        <el-input v-model="listQuery.name"></el-input>
      </el-form-item>

      <el-form-item label="属性数量:" prop="attribute_count">
        <el-input v-model="listQuery.attribute_count"></el-input>
      </el-form-item>
      <el-form-item label="参数数量:" prop="param_count">
        <el-input v-model="listQuery.param_count"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updateProductAttr } from "@/api/product.js";
export default {
  name: "productcateupdate",
  data() {
    return {
      listQuery: {
        id: this.$route.params.id,
        name: this.$route.params.name, //类型名称
        attribute_count: 0, //属性数量
        param_count: 0,
      }, //参数数量
      rules: {
        name: [
          { required: true, message: "请输入商品分类名称", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在3-100个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    console.log(18, this.$route.params.id);
    console.log(18, this.$route.params.name);
  },
  methods: {
    //添加商品分类
    submitForm: function (formName) {
      console.log("表单提交了");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 调用后台接口将数据传给服务器，生成一条新数据
          //表单验证通过
          console.log("表单验证通过", this.listQuery);
          updateProductAttr({id:this.listQuery.id,name:this.listQuery.name}).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                message: "成功修改商品",
                duration: 1000,
                type: "success",
              });
            }
            this.$router.push("/pms/productAttr");
          });
        } else {
          console.log("表单验证没通过");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.pro_con {
  padding: 10px;
  .box-card {
    margin-bottom: 10px;
  }
}
</style>